<template>
  <div>
    <template>
      <!-- this.$route 路由的参数对象 -->
      <!-- this.$router 路由导航对象 -->
      <div @click="$router.push('/artice/' + article.art_id)">
        <van-cell>
          <!-- 标题区域的插槽 -->
          <template #title>
            <div class="title-box">
              <!-- 标题 -->
              <span>{{ article.title }}</span>
              <!-- 单张图片 -->
              <img
                v-lazy="article.cover.images"
                alt=""
                class="thumb"
                v-if="article.cover.type === 1"
              />
            </div>
            <!-- 三张图片 -->
            <div class="thumb-box" v-if="article.cover.type === 3">
              <img
                v-lazy="item"
                alt=""
                class="thumb"
                v-for="(item, index) in article.cover.images"
                :key="index"
              />
            </div>
          </template>
          <!-- label 区域的插槽 -->
          <template #label>
            <div class="label-box">
              <span>
                {{ article.aut_name }} &nbsp;&nbsp; {{ article.comm_count }}评论
                &nbsp;&nbsp; 发布日期{{ article.pubdate | dataFormat }}
              </span>
              <!-- 关闭按钮 -->
              <!-- @click.stop 阻止冒泡 -->
              <van-icon name="cross" @click.stop="onClose" v-if="closable" />
            </div>
          </template>
        </van-cell>
        <!-- 第一个层:3个选项 -->
        <van-action-sheet
          v-model="show"
          cancel-text="取消"
          get-container="body"
        >
          <div>
            <van-cell
              :title="item.name"
              clickable
              class="center-title"
              v-for="item in actions"
              :key="item.id"
              @click="onActionClick(item)"
            />
          </div>
        </van-action-sheet>
        <!-- 第二个层:9个选项 -->
        <van-action-sheet
          v-model="show2"
          cancel-text="返回"
          @cancel="onAction2Click"
          get-container="body"
        >
          <div>
            <van-cell
              :title="item.label"
              clickable
              class="center-title"
              v-for="(item, i) in reports"
              :key="i"
              @click="reportAritcle(item.type)"
            />
          </div>
        </van-action-sheet>
      </div>
    </template>
  </div>
</template>
<script>
import reports from '../../api/reports'
import { dislikeArticleAPI, reportArticleAPI } from '../../api/homeApi'
export default {
  props: {
    article: {
      type: Object,
      require: true
    },
    closable: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      // 控制第一个的显示隐藏
      show: false,
      show2: false,
      actions: [
        { name: '不感兴趣' },
        { name: '反馈垃圾内容' },
        { name: '拉黑作者' }
      ],
      // 第二个面板的可选项
      reports
    }
  },
  created () {
  },
  methods: {
    async onActionClick (item) {
      // console.log(item)
      if (item.name === '不感兴趣') {
        // 调用api接口
        const { data: res } = await dislikeArticleAPI(this.article.art_id)
        if (res.message === 'OK') {
          console.log(res)
        }
        this.$emit('remove-article', this.article.art_id)
        this.show = false
      } else if (item.name === '反馈垃圾内容') {
        this.show = false
        this.show2 = true
      } else if (item.name === '拉黑作者') {

      }
    },
    onAction2Click () {
      this.show2 = false
      this.show = true
    },
    // 点击了关闭的小图标
    onClose () {
      this.show = !this.show
    },
    // 举报文章
    async reportAritcle (type) {
      // 调用举报文章api
      const { data: res } = await reportArticleAPI(this.article.art_id, type)
      if (res.message === 'OK') {
        console.log('举报文章成功')
        this.$emit('remove-article', this.article.art_id)
      }
    }
  }

}
</script>

<style lang="less" scoped>
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.thumb {
  // 矩形黄金比例：0.618
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.thumb-box {
  display: flex;
  justify-content: space-between;
}
.center-title {
  text-align: center;
}
</style>
